package com.niannian.activity;

import java.io.File;

import com.niannian.view.PhotoViewAttacher;
import com.niannian.view.PhotoViewAttacher.OnViewTapListener;
import com.niannian.imgCache.core.BitmapDecoder;
import com.niannian.util.BitmapUtil;
import com.niannian.util.Common;
import com.niannian.util.FileUtils;
import com.niannian.util.MD5Util;
import com.niannian.util.Util;
import com.niannian.util.network.QHttpUtil;
import com.niannian.BaseActivity;
import com.niannian.MyApplication;
import com.niannian.R;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class OriginalImage extends BaseActivity {
	private ImageView showImage;
	private ProgressBar LordImage;
	private PhotoViewAttacher mAttacher;
	private String imageURl;
	private Button delImg;
	private Bitmap showBitmap;
	private boolean isDelImg = false;
	private byte[] data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addActToGroup("ALL", this);
		setContentView(R.layout.view_original_image);
		initView();
		if (!isDelImg && !TextUtils.isEmpty(imageURl)) {
			File sdimage = new File(MyApplication.FILE_PATH_TEMP + MD5Util.encode(imageURl) + FileUtils.getImageFileExtension(imageURl));

			if (!sdimage.exists()) {
				LordImage.setVisibility(View.VISIBLE);
				new downloadImage().execute(imageURl);
			} else {
				try {
					data = FileUtils.getBytes(sdimage);
					showBitmap = BitmapDecoder.decodeSampledBitmapFromByteArray(data, 0, data.length, mScreenWidth, mScreenHeight);
					showImage.setImageBitmap(showBitmap);
					mAttacher = new PhotoViewAttacher(showImage);
				} catch (Exception e) {
					e.printStackTrace();
				}
				mAttacher.setOnViewTapListener(new OnViewTapListener() {
					@Override
					public void onViewTap(View view, float x, float y) {
						OriginalImage.this.finish();
					}

				});
			}
		} else {
			showBitmap = BitmapFactory.decodeFile(imageURl);
			showImage.setImageBitmap(showBitmap);
			mAttacher = new PhotoViewAttacher(showImage);
			mAttacher.setOnViewTapListener(new OnViewTapListener() {
				@Override
				public void onViewTap(View view, float x, float y) {
					OriginalImage.this.finish();
				}
			});
		}
		delImg.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				/*
				 * Intent i = new Intent(OriginalImage.this,
				 * PublishBaGua.class); showImage.setImageBitmap(null);
				 * setResult(RESULT_OK, i); OriginalImage.this.finish();
				 */
			}
		});
	}

	private void initView() {
		delImg = (Button) findViewById(R.id.btn_delImg);
		LordImage = (ProgressBar) findViewById(R.id.loadimage);
		showImage = (ImageView) findViewById(R.id.imageview);
		Bundle b = getIntent().getExtras();
		if (b != null) {
			imageURl = b.getString("imgurl");
			if (!Common.empty(imageURl)) {
				isDelImg = true;
				delImg.setVisibility(View.VISIBLE);
			} else {
				try {
					imageURl = getIntent().getStringExtra("OriginalImageUrl");
					String displayurl = getIntent().getStringExtra("display");
					if (displayurl != null) {
						MyApplication.finalbitmap.display(showImage, displayurl);
					} else {
						MyApplication.finalbitmap.display(showImage, imageURl);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				// if (!Util.isUrl(imageURl)) {
				// imageURl = FinalBitmap.getOriginalImage(imageURl);
				// }
			}
		}
	}

	@Override
	protected void onDestroy() {
		if (mAttacher != null) {
			mAttacher.cleanup();
		}
		if (showBitmap != null) {
			showBitmap.recycle();
		}
		super.onDestroy();
	}

	private class downloadImage extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			LordImage.setProgress(values[0]);
		}

		@Override
		protected String doInBackground(String... params) {
			Log.i("doInBackground", params[0]);
			data = QHttpUtil.download_image1(params[0]);
			return null;
		}

		@Override
		protected void onPostExecute(String uri) {
			super.onPostExecute(uri);
			try {
				LordImage.setVisibility(View.GONE);
				if (data != null) {
					showBitmap = BitmapDecoder.decodeSampledBitmapFromByteArray(data, 0, data.length, mScreenWidth, mScreenHeight);
					showImage.setImageBitmap(showBitmap);
				} else {
					Log.w("onPostExecute", "download null data for display image");
				}
				String savpic = MyApplication.FILE_PATH_TEMP + MD5Util.encode(imageURl) + FileUtils.getImageFileExtension(imageURl);
				BitmapUtil.saveBmp(showBitmap, savpic, 100);

				if (!new File(savpic).exists()) {
					Common.tip(OriginalImage.this, "图片保存失败");
				}
				mAttacher = new PhotoViewAttacher(showImage);
				mAttacher.setOnViewTapListener(new OnViewTapListener() {
					@Override
					public void onViewTap(View view, float x, float y) {
						OriginalImage.this.finish();
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}
